package day11;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class XmlRead {
    public static void main(String[] args) throws DocumentException {
        SAXReader reader=new SAXReader();
        Document doc = reader.read(new File("E:\\java_workspace\\IdeaProjects\\java75-java\\advanced\\src\\day11\\courses.xml"));
        Element root = doc.getRootElement();
        System.out.println(root.getName());
        //root.selectNodes("book");
        List<Book> books=new ArrayList<>();

        List<Element> list = doc.selectNodes("/books/book");
        for(Element e:list){
            String p = e.attributeValue("publish");
            String order=e.attributeValue("order");
            //List<Element> listname = e.selectNodes("name");
            //Element ename=listname.get(0);
            Element ename= (Element) e.selectSingleNode("name");
            String name=ename.getText().trim();
            Element eprice=(Element) e.selectSingleNode("price");
            double price=Double.parseDouble(eprice.getText());
            Element edes= (Element) e.selectSingleNode("description");
            String desc=edes.getText().trim();
            System.out.printf("name:%s,price %f,描述:%s\n",name,price,desc);

            Book b=new Book();
            b.setName(name);
            b.setDesc(desc);
            b.setPrice(price);
            books.add(b);
        }
        System.out.println("书籍信息如下:");
        System.out.println(books);
    }
}
